<template>
  <div class="container">
    <!-- 仅增加标签 -->
    <el-dialog
      v-if="routeData.id"
      title="新增标签"
      :visible="dialogVisible"
      width="30%"
      @close="closeDialog"
      @closed="closed"
    >
      <el-form
        label-width="100px"
        ref="tagsAddForm"
        :model="tagsAddForm"
        :rules="rules"
      >
        <el-form-item label="标签名称" prop="tagName">
          <el-input
            style="width: 300px"
            v-model="tagsAddForm.tagName"
            placeholder="请输入标签名称"
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="closeDialog">取 消</el-button>
        <el-button type="primary" @click="submitDialog">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 选择学科+标签 -->
    <el-dialog
      v-else
      :title="showTitle"
      :visible="dialogVisible"
      width="30%"
      @close="closeDialog"
      @closed="closed"
      @open="getSimpleSub"
    >
      <el-form
        label-width="100px"
        ref="tagsAddForm"
        :model="tagsAddForm"
        :rules="rules"
      >
        <el-form-item label="所属学科" prop="subjectID">
          <el-select
            style="width: 300px"
            v-model="tagsAddForm.subjectID"
            placeholder="请选择学科名称"
          >
            <el-option
              v-for="item in simpleSubList"
              :key="item.value"
              :value="item.value"
              :label="item.label"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="标签名称" prop="tagName">
          <el-input
            style="width: 300px"
            v-model="tagsAddForm.tagName"
            placeholder="请输入标签名称"
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="closeDialog">取 消</el-button>
        <el-button type="primary" @click="submitDialog">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { simple } from '@/api/hmmm/subjects.js'
import { add, update } from '@/api/hmmm/tags.js'
export default {
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    },
    tagsAddForm: {
      type: Object,
      required: true
    },
    routeData: {
      type: Object,
      required: false
    }
  },
  data () {
    return {
      rules: {
        subjectID: [
          { required: true, message: '请选择学科名称', trigger: 'change' }
        ],
        tagName: [
          { required: true, message: '请输入标签名称', trigger: 'blur' }
        ]
      },
      // 学科列表
      simpleSubList: []
    }
  },

  methods: {
    closeDialog () {
      this.$emit('update:dialogVisible', false)
    },

    closed () {
      this.$emit('closed')
      this.$refs.tagsAddForm.resetFields()
    },

    // Dialog open获取学科列表
    async getSimpleSub () {
      const { data: res } = await simple()
      // console.log(res)
      // if (this.tagsAddForm.subjectID) {
      //   const res1 = res.filter(
      //     (item) => item.value === this.tagsAddForm.subjectID
      //   )
      //   // console.log(res1[0].value)
      //   this.tagsAddForm.subjectName = res1[0].value
      // }
      this.simpleSubList = res
    },

    // Dialog提交按钮
    submitDialog () {
      this.$refs.tagsAddForm.validate(async (flag) => {
        if (!flag) return
        if (this.tagsAddForm.id) {
          await update(this.tagsAddForm)
          this.closeDialog()
          this.closed()
          this.$message.success('修改标签成功')
        } else {
          if (this.routeData.id) {
            this.tagsAddForm.subjectID = +this.routeData.id
            await add(this.tagsAddForm)
            /// console.log(res)
            this.closeDialog()
            this.closed()
            this.$message.success('新增标签成功')
          } else {
            await add(this.tagsAddForm)
            /// console.log(res)
            this.closeDialog()
            this.closed()
            this.$message.success('新增标签成功')
          }
        }
        this.$emit('getTagList')
      })
    }
  },

  computed: {
    showTitle () {
      if (this.tagsAddForm.id) {
        return '修改标签'
      } else {
        return '新增标签'
      }
    }
  }
}
</script>

<style scoped lang="scss">
::v-deep {
  .el-dialog__footer {
    text-align: right;
  }
}
</style>
